﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ERP_RORZE_SYSTEM;
using System.Data;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class rpt_StampPrint : DevExpress.XtraReports.UI.XtraReport
    {
        public string serialno_from;
        public string serialno_to;
        public bool comp;
        public rpt_StampPrint()
        {
            InitializeComponent();
            this.serialno_from = null;
            this.serialno_to = null;
            this.comp = false;
        }

        public void BindData()
        {
            modelNo.DataBindings.Add("Text", DataSource, "CompID");
            serialNo.DataBindings.Add("Text", DataSource, "SerialNo");
            modelNo1.DataBindings.Add("Text", DataSource, "CompID");
            serialNo1.DataBindings.Add("Text", DataSource, "SerialNo");
        }

        public void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = null;
            if (this.comp)
            {
                sql = "SELECT SUBSTRING(t1.[SerialNo], 1, 7) AS [SerialNo],"
                    + " ISNULL(t2.[CompName], t1.[CompID]) AS [CompID]"
                    + " FROM [231_RobotOrderDetail] AS t1 LEFT JOIN"
                    + " [231_Comp] AS t2 ON t1.[CompID] = t2.[CompID]"
                    + " WHERE (t1.[SerialNo] BETWEEN '" + this.serialno_from + "' AND '" + this.serialno_to + "')"
                    + " AND (LEN(t1.[SerialNo]) <= 8)";
            }
            else
            {
                sql = "SELECT SUBSTRING([SerialNo], 1, 7) AS [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE ([SerialNo] BETWEEN '" + this.serialno_from + "' AND '" + this.serialno_to + "')"
                    + " AND (LEN([SerialNo]) <= 8)";
            }
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                try
                {
                    this.DataSource = sourceDataSet.Tables[0];
                    BindData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Cannot Load data to report");
            }
        }
    }
}
